package in.muton.edushare.components.autocomplete.api;

import in.muton.edushare.components.calendar.api.UniqueElementList;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UsersDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.GenericAudience;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.Users;


public class UserHelper extends NewAudienceHelper {
	private static final long serialVersionUID = 7489082168554030740L;

	public UserHelper(ORMInterface orm) {
		userList.add(new UsersDAO(orm).getAllUsers());
	}

	public List<String> getStringList() {
		List<String> list = new ArrayList<String>();
		List<IModel<Users>> temp = userList.getObject();
		for (IModel<Users> t : temp) {
			list.add("[" + t.getObject().getCompleteUserName() + ","
					+ t.getObject().getEmail() + "]; ");
		}
		return list;
	}

	public IModel<Users> getUser(String s) {
		List<IModel<Users>> ci = userList.getObject();
		for (IModel<Users> c : ci) {
			if (s.contains("[") || s.contains("]")) {
				if (s.contains(c.getObject().getCompleteUserName())
						&& s.contains(c.getObject().getEmail()))
					return c;
			} else if (s.equals(c.getObject().getUserName())
					|| s.equals(c.getObject().getEmail())) {
				return c;
			}
		}
		return null;
	}

	public CayenneList<Users> getAllUsers(String s) {
		CayenneList<Users> u = new CayenneList<Users>(null);
		IModel<Users> user = getUser(s);
		if(user != null)
			u.add(user);
		return u;
	}

	public CayenneList<GenericAudience> getAudiences(String s) {
		return null;
	}

	private UniqueElementList<Users> userList = new UniqueElementList<Users>(
			null);

	@Override
	public String getNameForAudience(String s) {
		return getUser(s).getObject().getCompleteUserName();
	}

	@Override
	public int getRoleId(String s) {
		return Poll.POLL_INDI;
	}
}
